package com.bytedance.frameworks.baselib.network.dispatcher;

import android.os.Process;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.frameworks.baselib.network.dispatcher.IRequest.Priority;

import java.util.concurrent.BlockingQueue;

final class ApiLocalDispatcher extends Thread {
    volatile boolean a;
    private final BlockingQueue b;
    private final BlockingQueue c;

    public ApiLocalDispatcher(BlockingQueue arg2, BlockingQueue arg3) {
        super("ApiLocalDispatcher-Thread");
        this.a = false;
        this.b = arg2;
        this.c = arg3;
    }

    public final void run() {
        Object v1;
        Object task;
        Process.setThreadPriority(10);
        do {
            try {
                task = this.b.take();
            } catch (InterruptedException ex) {
                if (!this.a) {
                    continue;
                } else {
                    return;
                }
            }

            if (task == null) {
                continue;
            }

            if (!(task instanceof AbsRequest)) {
                continue;
            }

            String v1_1 = Thread.currentThread().getName();
            String v2 = ((AbsRequest) task).getName();
            if (((AbsRequest) task).isCanceled()) {
                continue;
            }

            if (!android.support.design.a.isTextEmpty(v2) && !android.support.design.a.isTextEmpty(v1_1)) {
                Thread.currentThread().setName("ApiLocalDispatcher-" + v2);
            }

            if (Logger.debug()) {
                Logger.d("ApiLocalDispatcher", "run4Local " + v2 + ", queue size: " + this.b.size() + " " + this.c.size());
            }

            try {
                if (((AbsRequest) task).run4Local()) {

//                goto label_49;
                } else if (((AbsRequest) task).getPriority() == Priority.IMMEDIATE) {
                    ThreadPlus.submitRunnable(((Runnable) task));
//                goto label_49;
                } else {
                    this.c.add(task);
                }
            } catch (Exception e) {
                Logger.e("ApiLocalDispatcher", "Unhandled exception: " + e);
            }

            if (android.support.design.a.isTextEmpty(v2)) {
                continue;
            }
            if (android.support.design.a.isTextEmpty(v1_1)) {
                continue;
            }
            Thread.currentThread().setName(v1_1);
            continue;
        } while (true);
    }
}

